package com.suncnpap.system.controller;
import com.app.util.common.HandlerResult;
import com.app.util.common.ResponseUtil;
import com.github.pagehelper.PageInfo;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.system.model.*;
import com.suncnpap.system.service.TMakeMakeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-12-15 16:40:02
 */
@RestController
@RequestMapping("/tmakemake")
public class TMakeMakeController extends BaseController {

    @Autowired
    private TMakeMakeService tMakeMakeService;

    /**
     * 预约功能
     */
    @RequestMapping(value ="/make")
    public void make(@RequestBody(required = false) TMakeMake tMakeMake, HttpServletResponse response,
                     HttpServletRequest request)throws Exception {
        HandlerResult result = this.checkToken(request);
        if(result.getCode()==0){
            tMakeMake.setCreationDate(new Date());
            tMakeMake.setUpdateDate(new Date());
            tMakeMake.setOverdueDate(DateAfter.getDateAfter(tMakeMake.getCreationDate(),2));
            Integer make=tMakeMakeService.getMake(tMakeMake);
            if(make>0){
                result.setCode(0);
                result.setMessage("预约成功,请在两天之内去取书，否则将会自动取消预约");
                ResponseUtil.writeJson(result, response);
                return;
            }
        }
        ResponseUtil.writeJson(result, response);
    }

    /**
     * 查看是否被预约
     * @param id
     * @param response
     * @throws Exception
     */
    @RequestMapping(value ="/ismake/{id}")
    public void isMake(@PathVariable String id, HttpServletResponse response)throws Exception{
        HandlerResult result = new HandlerResult();
        if(result.getCode()==0){
            TMakeMake tMakeMake=tMakeMakeService.isMake(id);
            if(tMakeMake==null){
                result.put("tMakeMake","");
            }else {
                result.put("tMakeMake",tMakeMake);
            }
        }
        ResponseUtil.writeJson(result, response);
    }

    /**
     * 我的预约
     * @param makeVo
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping(value ="/makelist")
    public void getMakeList(@RequestBody MakeVo makeVo, HttpServletResponse response,HttpServletRequest request)throws Exception{
        HandlerResult result = this.checkToken(request);
        if(result.getCode()==0){
            PageInfo<TMakeMake> tMakeMake = tMakeMakeService.makeList(makeVo);
            List<TMakeMake> list = tMakeMake.getList();
            int pageNum = tMakeMake.getPageNum();
            int pageSize = tMakeMake.getPageSize();
            long total = tMakeMake.getTotal();
            result.put("list",list);
            result.put("pageNum",pageNum);
            result.put("pageSize",pageSize);
            result.put("total",total);
        }
        ResponseUtil.writeJson(result, response);
    }

    /**
     * 取消预约
     * @param cancelMakeVo
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping(value ="/cancelmake")
    public void cancelMake(@RequestBody CancelMakeVo cancelMakeVo, HttpServletResponse response, HttpServletRequest request)throws Exception{
        HandlerResult result = this.checkToken(request);
        if(result.getCode()==0){
            Integer cancel=tMakeMakeService.cancelMake(cancelMakeVo);
            if(cancel>0){
               result.setCode(0);
               result.setMessage("取消预约成功");
               ResponseUtil.writeJson(result, response);
               return;
            }
        }
        ResponseUtil.writeJson(result, response);
    }

    @RequestMapping(value ="/makes/{strSchool}")
    public void cancelMake(@RequestBody List<Makes> makes, @PathVariable String strSchool,
                           HttpServletResponse response, HttpServletRequest request)throws Exception{
        System.out.println(makes);
          HandlerResult result = new HandlerResult();
//        List<Makes>request.getParameter("makes");
//        HandlerResult result = this.checkToken(request);
        List<String> array = new ArrayList<>();
        if(result.getCode()==0){
            for (Makes make:makes){
                TMakeMake tMakeMake=tMakeMakeService.makes(make.getStrBookBarcode(),strSchool,make.getStrReaderCardNumber());
                if(tMakeMake!=null){
                    array.add("预约中");
                }else {
                    array.add("未预约");
                }
            }
            result.put("makeInfo",array);
            ResponseUtil.writeJson(result, response);
            return;
        }
    }
}
